package com.itools.core.config;

import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @project: itools-backend
 * @description: 配置了minio的文件策略才会引用
 * @author: XUCHANG
 * @create: 2021-04-01 16:06
 */
@Data
@Component
@ConditionalOnProperty(prefix = "fms",name = "strategy",havingValue = "minio")
@ConfigurationProperties(prefix = "fms.minio")
@Log4j2
public class MinioPropertiesConfig {

    @ApiModelProperty("endPoint是一个URL，域名，IPv4或者IPv6地址")
    private String endpoint;

    @ApiModelProperty("TCP/IP端口号")
    private int port;

    @ApiModelProperty("accessKey类似于用户ID，用于唯一标识你的账户")
    private String accessKey;

    @ApiModelProperty("secretKey是你账户的密码")
    private String secretKey;

    @ApiModelProperty("如果是true，则用的是https而不是http,默认值是true")
    private Boolean secure;

    @ApiModelProperty("默认存储桶")
    private String bucketName;

    @ApiModelProperty("配置目录")
    private String configDir;

    @Bean
    @ConditionalOnProperty(prefix = "fms",name = "strategy",havingValue = "minio")
    public MinioClient initMinioClient() {
        MinioClient minioClient = null;
        log.info("初始化注入MinioClient");
        try {
            minioClient = new MinioClient(endpoint, port, accessKey, secretKey,secure);
        } catch (InvalidEndpointException e) {
            e.printStackTrace();
        } catch (InvalidPortException e) {
            e.printStackTrace();
        }
        return minioClient;
    }

}